<template>
	<view class="body">
    <nav-bar title="社区发布"></nav-bar>
		<view class="box">
			<!-- 标题 -->
			<view class="">
				<input type="text" v-model="communityContent.articleTitle" placeholder="输入一个醒目的标题~" maxlength="40" />
			</view>
			<!-- 文章内容 -->
			<view class="content">
				<textarea class="JTxtArea" v-model="communityContent.articleContent" :data-maxnum="communityContent.articleContent.length+'/500'"
					@input="inputReason" placeholder="分享今天的故事~" maxlength="500" :maxlength="500" />
			</view>
			<view class="wenben"> 默认第一张为主图哦~ </view>

			<!-- 图片区域 -->
			<image-upload :objUser="objUser" @imgRegion="showOverlay" @imgpath="upImgPath" />
			<!-- 选择标签区域 	-->
			<view class="huati"> 添加话题 </view>
			<view class="uplabel">
				<view :class="isActive===index? 'clazx' : 'uplabel_text'" v-for="(item,index) in label"
					:key="item.familyId" @click="selectLabel(item.familyId,index)">
					{{item.familyName}}

				</view>
			</view>
			<!-- 发布按钮！ -->
			<view class="release">
				<view class="release_button" @click="releaseButton">
					发布
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				communityContent:{
					// 文章标题
					articleTitle: "",
					// 文章内容
					articleContent: "",
					// 标签ID
					familyId: '',
					imgpath: []
				},
				// 标签
				label: [],
				isActive: '',
				// 图片展示
				imgRegion: [],
				objUser: ''
			}
		},
		onLoad() {
			this.chauxn()
		},
		created() {
			// 拿到用户信息
			try {
				const value = uni.getStorageSync('userObj');
				this.objUser = value.token
			} catch (e) {}
		},
		methods: {
			showOverlay(imgRegion) {
				console.log(imgRegion)
				this.imgRegion = imgRegion
			},
			upImgPath(e) {
				console.log(e)
				this.communityContent.imgpath = e
			},

			inputReason(e) {
				this.reason = e.detail.value;
			},
			
			// 查询标签
			async chauxn() {
				const {
					data: res
				} = await uni.$http.get('/ym_server/article-family/getArticleFamily')
				this.label = res
			},
			// 选择标签
			selectLabel(a, index) {
				console.log(index)
				this.isActive = index
				// 传入标签id
				this.communityContent.familyId = a
			},

			// 添加文章
			releaseButton() {
				if (this.communityContent.articleTitle == '' || this.communityContent.articleContent == '') {
					uni.showToast({
						title: '标题不允许为空',
						icon: 'none',
						duration: 2000
					});
				} else if (this.communityContent.familyId == '') {
					uni.showToast({
						title: '请选择发布标签',
						icon: 'none',
						duration: 2000
					});
				} else {
					uni.$http.post('/ym_server/IntactArticle', {
						"familyId": this.communityContent.familyId,
						"title": this.communityContent.articleTitle,
						"content": this.communityContent.articleContent,
						"imageLink": this.communityContent.imgpath
					}).then((res) => {
						console.log(res)
						console.log(res.data.code)
						if (res.data.code === 200) {
							this.communityContent.familyId = '',
							this.communityContent.articleTitle = '',
							this.communityContent.articleContent = ''
							setTimeout(function() {
								uni.showToast({
									title: '文章发布成功',
									duration: 2000
								})
							}, 1000)
							setTimeout(function() {
								uni.switchTab({
									url: `../../pages/community/community?articleId=${res.data.data.id}`
								})
							}, 2000)
							uni.setStorageSync('id', JSON.stringify(res.data.data.id))
						} else {
							console.log("文章发布失败")
							setTimeout(function() {
								uni.showToast({
									title: '文章发布失败',
									duration: 2000,
									icon:'error'
								})
							}, 1000)
						}
					})
					console.log("上传完毕！")
				}

			},

		},
	}
</script>

<style lang="scss">
	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
		color: transparent;
	}

	.returnBox {
		position: sticky;
		background-color: #ffffff;
		padding-bottom: 40rpx;
		z-index: 999;
		top: 0px;
		left: 0px
	}

	.body {
		font-family: "黑体";
		background-color: #FFFFFF;
		min-height: 100vh;
	}

	.box {
		font-family: "黑体";
		padding: 0rpx 40rpx;

		input {
			width: 100%;
			height: 100rpx;
			background-color: #FFFFFF;
			font-size: 40rpx;
		}

		.content {
			border-top: 4rpx solid #AEAEAE;
			background-color: #FFFFFF;
			font-size: 32rpx;
			padding-top: 20rpx;

			.JTxtArea {
				border-radius: 5px;
				width: calc(100% - 16px);
				padding: 16rpx 16rpx 40rpx 16rpx;
				line-height: 40rpx;
				color: #000000;
				position: relative;
				height: 500rpx;
				border-bottom: 4rpx solid #AEAEAE;

				&:after {
					content: attr(data-maxnum);
					position: absolute;
					right: 10rpx;
					bottom: 0rpx;
					font-size: 28rpx;
					color: gray;
				}
			}



			image {
				width: 100%;
			}
		}

		.wenben {
			color: #AEAEAE;
			font-size: 26rpx;
			width: 100%;
			margin-top: 20rpx;
		}

		.huati {
			font-size: 34rpx;
			width: 100%;
			margin-top: 20rpx;
			font-weight: bold;
		}

		.uplabel {
			width: 100%;
			margin-bottom: 50rpx;
			min-height: 100rpx;
				font-size: 24rpx;
			color: #FFFFFF;

			.uplabel_text {
				margin-top: 30rpx;
				float: left;
				margin-right: 20rpx;
				background-color: #cccccc;
				max-width: 140rpx;
				border-radius: 32rpx;
				text-align: center;
				padding: 8rpx 12rpx;
				width: 160rpx;
				height: 44rpx;
				line-height: 44rpx;
			}

			.clazx {
				margin-top: 30rpx;
				float: left;
				margin-right: 20rpx;
				background-color: #3FD3D1;
				max-width: 140rpx;
				border-radius: 32rpx;
				text-align: center;
				padding: 8rpx 12rpx;
				width: 160rpx;
				height: 44rpx;
				line-height: 44rpx;
			}
		}

		.release {
			width: 100%;
			height: 110rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			padding-bottom: 30rpx;
			margin-top: 140rpx;

			.release_button {
				background-color: #3fd3d1;
				color: #FFFFFF;
				width: 95%;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 16rpx;
				font-size: 36rpx;
				text-align: center;
			}
		}
	}
</style>
